package com.event.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.event.exception.UserException;
import com.event.mapper.UserMapper;
import com.event.pojo.dto.UserDTO;
import com.event.pojo.entity.User;
import com.event.service.UserService;
import com.event.utils.JwtUtil;
import com.event.utils.Md5Util;
import com.event.utils.ThreadLocalUtil;
import com.google.common.base.Strings;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author ihuwe
 * @description 针对表【user(用户表)】的数据库操作Service实现
 * @createDate 2023-12-18 20:27:16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
	implements UserService {
	
	@Resource
	private RedisTemplate<String, String> redisTemplate;
	
	/**
	 * 注册
	 */
	@Override
	public void register(String username, String password) {
		if (getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username)) != null) {
			throw new RuntimeException(UserException.UAERNAME_EXIST);
		}
		save(User.builder().username(username).password(Md5Util.getMD5String(password)).createTime(LocalDateTime.now()).updateTime(LocalDateTime.now()).build());
	}
	
	/**
	 * 登录
	 */
	@Override
	public String login(String username, String password) {
		if (getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username)) == null) {
			throw new RuntimeException(UserException.UAERNAME_NOT_EXIST);
		}
		User user = getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
		if (!Md5Util.checkPassword(password, user.getPassword())) {
			throw new RuntimeException(UserException.PASSWORD_ERROR);
		}
		HashMap<String, Object> map = new HashMap<>();
		map.put("id", user.getId());
		map.put("username", user.getUsername());
		String token = JwtUtil.genToken(map);
		redisTemplate.opsForValue().set("token" + user.getId(), token, 1, TimeUnit.DAYS);
		return token;
	}
	
	@Override
	public void update(User user) {
		update(Wrappers.<User>lambdaUpdate()
			.eq(User::getId, user.getId())
			.eq(!Strings.isNullOrEmpty(user.getUsername()), User::getUsername, user.getUsername())
			.set(!Strings.isNullOrEmpty(user.getNickname()), User::getNickname, user.getNickname())
			.set(!Strings.isNullOrEmpty(user.getEmail()), User::getEmail, user.getEmail())
			.set(User::getUpdateTime, LocalDateTime.now()));
	}
	
	@Override
	public void updateAvatarAvatar(String url) {
		if (Strings.isNullOrEmpty(url)) {
			throw new RuntimeException(UserException.USER_PIC_ERROR);
		}
		update(Wrappers.<User>lambdaUpdate().eq(User::getId, ThreadLocalUtil.get()).set(User::getUserPic, url).set(User::getUpdateTime, LocalDateTime.now()));
	}
	
	@Override
	public void updatePwd(UserDTO userDTO) {
		User user = getById(ThreadLocalUtil.get());
		if (!Md5Util.checkPassword(userDTO.getOldPassword(), user.getPassword())) {
			throw new RuntimeException(UserException.ORIGINAL_PASSWORD_ERROR);
		}
		if (!Md5Util.checkPassword(userDTO.getNewPassword(), user.getPassword())) {
			throw new RuntimeException(UserException.NEW_AND_OLD_NOT_EQUAL);
		}
		if ((userDTO.getNewPassword().length() < 6 || userDTO.getNewPassword().length() > 16) ||
			(userDTO.getConfirmPassword().length() < 6 || userDTO.getConfirmPassword().length() > 16)) {
			throw new RuntimeException(UserException.PASSWORD_LENGTH_ERROR);
		}
		if (!userDTO.getNewPassword().equals(userDTO.getConfirmPassword())) {
			throw new RuntimeException(UserException.PASSWORD_NOT_EQUAL);
		}
		update(Wrappers.<User>lambdaUpdate()
			.eq(User::getId, user.getId())
			.set(User::getPassword, Md5Util.getMD5String(userDTO.getNewPassword()))
			.set(User::getUpdateTime, LocalDateTime.now()));
	}
}




